﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace KetBanBonPhuong
{
    /// <summary>
    /// Summary description for Group
    /// </summary>
    public class Group
    {
        private int group_id;

        public int Group_id
        {
            get
            {
                return group_id;
            }
            set
            {
                group_id = value;
            }
        }


        private int user_id;

        public int User_id
        {
            get
            {
                return user_id;
            }
            set
            {
                user_id = value;
            }
        }

        private string group_name;

        public string Group_name
        {
            get
            {
                return group_name;
            }
            set
            {
                group_name = value;
            }
        }

        private string about_group;

        public string About_group
        {
            get
            {
                return about_group;
            }
            set
            {
                about_group = value;
            }
        }

        private string interests;

        public string Interests
        {
            get
            {
                return interests;
            }
            set
            {
                interests = value;
            }
        }

        private string phone;

        public string Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;
            }
        }

        private bool is_activated;

        public bool Is_activated
        {
            get
            {
                return is_activated;
            }
            set
            {
                is_activated = value;
            }
        }

        private DateTime created_date;

        public DateTime Created_date
        {
            get
            {
                return created_date;
            }
            set
            {
                created_date = value;
            }
        }

        public Group()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Group(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                group_id = sdr.GetInt32(0);
                if (sdr.IsDBNull(1))
                    user_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    group_name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    about_group = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    interests = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    phone = sdr.GetString(5);
                if (!sdr.IsDBNull(6))
                    is_activated = sdr.GetBoolean(6);
                if (!sdr.IsDBNull(7))
                    created_date = sdr.GetDateTime(7);
            }
        }

        public static DataTable GetAll()
        {
            DBHelp dbh = new DBHelp();
            DataTable dt = new DataTable();
            string cmd = "Group_GetAll";
            dt = dbh.ExecuteDataTable(cmd, CommandType.StoredProcedure);
            return dt;
        }

        public static DataTable GetGroupByMemberID(int MemberID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@member_user_id", MemberID);
            DataTable dt = new DataTable();
            string cmd = "select distinct g.group_id, g.group_name, m.member_user_id " +
                "from tbl_group g inner join tbl_member_list ml " +
                "on g.group_id = ml.group_id inner join tbl_member m " +
                "on ml.member_list_id = m.member_list_id " +
                "where m.member_user_id = @member_user_id";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        public static DataTable GetGroupSuggestByUserID(int UserID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", UserID);
            DataTable dt = new DataTable();
            string cmd = "select * from tbl_group g inner join tbl_member_list ml " +
                "on g.group_id = ml.group_id where ml.member_list_id in " +
                "(select distinct m.member_list_id from tbl_member m inner join tbl_friend f " +
                "on m.member_user_id = f.friend_user_id	inner join tbl_friend_list fl " +
                "on f.friend_list_id = fl.friend_list_id where fl.user_id = @user_id)";
            dt = dbh.ExecuteDataTable(cmd, CommandType.Text);
            return dt;
        }

        //Ham lay ve ID nhan vao
        public static Group GetInfo(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@group_id", ID);
            Group group = new Group();
            string cmd = "Group_GetInfo";
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if (sdr.Read())
            {
                group.Group_id = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    group.User_id = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    group.Group_name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    group.About_group = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    group.Interests = sdr.GetString(4);
                if (!sdr.IsDBNull(5))
                    group.Phone = sdr.GetString(5);
                if (!sdr.IsDBNull(6))
                    group.Is_activated = sdr.GetBoolean(6);
                if (!sdr.IsDBNull(7))
                    group.Created_date = sdr.GetDateTime(7);
            }
            return group;
        }

        //Them group
        public static void Insert(Group group)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@user_id", group.User_id);
            dbh.AddParameter("@group_name", group.Group_name);
            dbh.AddParameter("@about_group", group.About_group);
            dbh.AddParameter("@interests", group.Interests);
            dbh.AddParameter("@phone", group.Phone);
            dbh.AddParameter("@is_activated", group.Is_activated);
            dbh.AddParameter("@created_date", group.Created_date);
            string cmd = "Group_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Sua group
        public static void Update(Group group)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@group_id", group.Group_id);
            dbh.AddParameter("@user_id", group.User_id);
            dbh.AddParameter("@group_name", group.Group_name);
            dbh.AddParameter("@about_group", group.About_group);
            dbh.AddParameter("@interests", group.Interests);
            dbh.AddParameter("@phone", group.Phone);
            dbh.AddParameter("@is_activated", group.Is_activated);
            dbh.AddParameter("@created_date", group.Created_date);
            string cmd = "Group_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        //Xoa group
        public static void Delete(int ID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@group_id", ID);
            string cmd = "Group_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }

        // can thiet them sau
    }
}